package com.disgruntledbots;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.MassData;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.Shape;
import com.badlogic.gdx.physics.box2d.Shape.Type;

public class PhysicsJSONSerializer {

	public static String id = "Id";
	public static String position = "Position";
	public static String X = "X";
	public static String Y = "Y";
	public static String linearVelocity = "LinearVelocity";
	public static String angle = "Angle";
	public static String angularVelocity = "AngularVelocity";
	
	public static JSONObject SerializeBody(Body body, String name) throws JSONException, ClassCastException
	{
		JSONObject json = new JSONObject();
		json.put(id, name);
		
		json.put(position, SerializeVector2(body.getPosition()));
		json.put(linearVelocity, SerializeVector2(body.getLinearVelocity()));
		
		json.put(angle, SerializeFloatToHex(body.getAngle()));
		json.put(angularVelocity, SerializeFloatToHex(body.getAngularVelocity()));
		
		json.put("MassData", SerializeMassData(body.getMassData()));

		json.put("FixtureArray", SerializeFixtureList(body.getFixtureList()));

		return json;
	}
	
	public static JSONObject SerializeVector2(Vector2 vector) throws JSONException
	{
		JSONObject json = new JSONObject();
		json.put(X, SerializeFloatToHex(vector.x));
		json.put(Y, SerializeFloatToHex(vector.y));
		return json;
	}
	
	public static String SerializeFloatToHex(float single)
	{
		return Float.toHexString(single);
	}
	
	public static JSONObject SerializeMassData(MassData massDat) throws JSONException
	{
		JSONObject json = new JSONObject();
		
		json.put("Mass", SerializeFloatToHex(massDat.mass));
		json.put("Center", SerializeVector2(massDat.center));
		json.put("RotationalInertia", SerializeFloatToHex(massDat.I));
		
		return json;
	}
	
	public static JSONArray SerializeFixtureList(ArrayList<Fixture> fixtures) throws JSONException, ClassCastException
	{
		JSONArray json = new JSONArray();
		
		for(int i = 0; i < fixtures.size(); i++)
		{
			json.put(i, SerializeFixture(fixtures.get(i)));
		}
		
		return json;
	}
	
	public static JSONObject SerializeFixture(Fixture fixture) throws JSONException, ClassCastException
	{
		JSONObject json = new JSONObject();
		
		json.put("Density", SerializeFloatToHex(fixture.getDensity()));
		json.put("Friction", SerializeFloatToHex(fixture.getFriction()));
		json.put("Restition", SerializeFloatToHex(fixture.getRestitution()));
	
		json.put("Shape", SerializeShape(fixture.getShape()));
		
		return json;
	}
	
	public static JSONObject SerializeShape(Shape shape) throws JSONException, ClassCastException
	{
		JSONObject json = new JSONObject();
		
		if(shape.getType() == Type.Circle)
		{
			json.put("Circle", SerializeCircleShape((CircleShape)shape));
		}else{
			json.put("Polygon", SerializePolygonShape((PolygonShape)shape));
		}
		
		return json;
	}
	
	protected static JSONObject SerializeCircleShape(CircleShape circle) throws JSONException
	{
		JSONObject json = new JSONObject();
		
		json.put("Center", SerializeVector2(circle.getPosition()));
		json.put("Radius", SerializeFloatToHex(circle.getRadius()));
		
		return json;
	}
	
	protected static JSONObject SerializePolygonShape(PolygonShape poly) throws JSONException
	{
		JSONObject json = new JSONObject();
		JSONArray array = new JSONArray();
		Vector2 tmpVertex = new Vector2();
		
		for(int i = 0; i < poly.getVertexCount(); i++)
		{
			poly.getVertex(i, tmpVertex);
			array.put(i, SerializeVector2(tmpVertex));
		}
		
		json.put("VertexArray", array);
		
		return json;
	}
}
